<template>
  <div>
    <el-card style="margin: 20px 0px">
      <CategorySelect @getCategoryId="getCategoryId" :show="!isShowTable"></CategorySelect>
    </el-card>

    <el-card>
      <div v-show="isShowTable">
        <el-button type="primary" icon="el-icon-plus" :disabled="!category3Id" @click="addAttr">添加属性
        </el-button>
        <el-table style="width: 100%" border :data="attrList">
          <el-table-column
            type="index"
            align="center"
            prop="prop"
            label="序号"
            width="80"
          >
          </el-table-column>

          <el-table-column
            prop="attrName"
            label="属性名称"
            width="150"
          >
          </el-table-column>

          <el-table-column
            label="属性值列表"
            width="width"
            #default="{row, column, $index}"
          >
            <el-tag type="success" v-for="(attrValue, index) in row.attrValueList" :key="attrValue.id"
                    style="margin: 0px 20px"
            >{{ attrValue.valueName }}
            </el-tag>

          </el-table-column>

          <el-table-column
            prop="prop"
            label="操作"
            width="150"
            #default="{row, column, $index}"
          >
            <el-button type="warning" icon="el-icon-edit" size="mini" @click="updateAttr(row)"></el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini"></el-button>

          </el-table-column>
        </el-table>
      </div>
      <div v-show="!isShowTable">
        <el-form :inline="true" ref="form" label-width="80px" :model="attrInfo">
          <el-form-item label="属性名">
            <el-input placeholder="请输入属性名" v-model="attrInfo.attrName"></el-input>
          </el-form-item>
        </el-form>
        <el-button type="primary" icon="el-icon-plus" @click="addAttrValue" :disabled="!attrInfo.attrName">添加属性值
        </el-button>
        <el-button>取消</el-button>
        <el-table style="width: 100%; margin: 20px 0px" border :data="attrInfo.attrValueList">
          <el-table-column type="index" label="序号" width="80" align="center"/>
          <el-table-column prop="index" label="属性值名称" align="center">
            <template v-slot="{row, column, $index}">
              <el-input placeholder="请输入属性值名称" v-model="row.valueName" v-if="row.flag" @blur="toLook(row)"
                        @keyup.native.enter="row.flag=false" :ref="$index"
              ></el-input>
              <span v-else @click="toEdit(row, $index)">{{ row.valueName }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="index" label="操作" width="80" align="center">
            <template v-slot="{row, column, $index}">
              <el-popconfirm
                confirm-button-text="好的"
                cancel-button-text="不用了"
                icon="el-icon-info"
                icon-color="red"
                :title="`确定删除${row.valueName}？`"
                #reference
                @onConfirm="deleteAttrValue($index)"
              >
                <el-button type="danger" icon="el-icon-delete" size="mini"></el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-button type="primary" @click="addOrUpdateAttr" :disabled="!attrInfo.attrValueList.length">保存</el-button>
        <el-button @click="isShowTable=true">取消</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>


import cloneDeep from 'lodash/cloneDeep'
import { reqAddOrUpdateAttr } from '@/api/product/attr'

export default {
  name: 'Attr',
  data() {
    return {
      category1Id: '',
      category2Id: '',
      category3Id: '',
      attrList: [],
      flag: true,
      isShowTable: true,
      attrInfo: {
        attrName: '',
        attrValueList: [],
        categoryId: 0,
        categoryLevel: 3
      }
    }
  },
  methods: {
    // getCategoryId({categoryId, level}){
    //
    // },
    getCategoryId({ category1Id, category2Id, category3Id }) {
      this.category1Id = category1Id
      this.category2Id = category2Id
      this.category3Id = category3Id
      this.getAttrList()

    },
    async getAttrList() {
      const { category1Id, category2Id, category3Id } = this
      let result = await this.$API.attr.reqAttrList(category1Id, category2Id, category3Id)
      if (result.code == 200) {
        this.attrList = result.data
      }
    },
    addAttrValue() {
      this.attrInfo.attrValueList.push({
        attrId: this.attrInfo.id,
        valueName: '',
        flag: true
      })
      this.$nextTick(() => {
        this.$refs[this.attrInfo.attrValueList.length - 1].focus()
      })
    },
    addAttr() {
      this.isShowTable = false
      this.attrInfo = {
        attrName: '',
        attrValueList: [],
        categoryId: this.category3Id,
        categoryLevel: 3
      }
    },
    updateAttr(row) {
      this.isShowTable = false
      this.attrInfo = cloneDeep(row)
      this.attrInfo.attrValueList.forEach(item => {
        this.$set(item, 'flag', false)
      })
    },
    toLook(row) {
      if (row.valueName.trim() == '') {
        this.$message('请你输入一个正常属性值')
        return
      }
      let isRepeat = this.attrInfo.attrValueList.some(item => {
        if (row != item) {
          return row.valueName == item.valueName
        }
      })

      if (isRepeat) return

      row.flag = false
    },
    toEdit(row, index) {
      row.flag = true
      this.$nextTick(() => {
        this.$refs[index].focus()
      })
    },
    deleteAttrValue(index) {
      this.attrInfo.attrValueList.splice(index, 1)
    },
    async addOrUpdateAttr() {
      this.attrInfo.attrValueList = this.attrInfo.attrValueList.filter(item => {
        if (item.valueName != '') {
          delete item.flag
          return true
        }
      })
      try {
        await this.$API.attr.reqAddOrUpdateAttr(this.attrInfo)
        this.$message({ type: 'success', message: '保存成功' })
        this.getAttrList()
        this.isShowTable = true
      } catch (e) {

      }
    }
  }
}
</script>

<style scoped>

</style>
